/**
 * 项目控制器
 */
 import Koa from 'koa';

 import ProjectService from 'src/service/project';
 
 class ProjectController {
   private service: ProjectService = new ProjectService();
 
   // 获取列表
   list = async (ctx: Koa.Context) => {
     try {
       const { query } = ctx;
       const res = await this.service.getList(query as any);
       ctx.body = {
         data: res,
         status: true,
         message: '',
       };
     } catch (e) {
       ctx.logger.error(e);
       ctx.body = {
         data: {
           list: [],
           page: null,
         },
         status: false,
         message: (e as Error).message,
       };
     }
   };
 
   // 新建项目
   create = async (ctx: Koa.Context) => {
     try {
       const project = ctx.request.body;
       const newProject = await this.service.create(project);
       ctx.body = {
         data: { id: newProject.id },
         status: true,
         message: '新建成功',
       };
     } catch (e) {
       ctx.body = {
         data: null,
         status: false,
         message: (e as Error).message,
       };
     }
   };
 
   // 更新项目
   update = async (ctx: Koa.Context) => {
     try {
       const project = ctx.request.body;
       const id = `${ctx.query.id}`;
       await this.service.update(id, project);
       ctx.body = {
         data: { id: id },
         status: true,
         message: '更新成功',
       };
     } catch (e) {
       ctx.body = {
         data: null,
         status: false,
         message: (e as Error).message,
       };
     }
   };
 
   // 获取详情
   detail = async (ctx: Koa.Context) => {
     try {
       const id = `${ctx.query.id}`;
       const project = await this.service.detail(id);
       ctx.body = {
         data: project,
         status: true,
         message: '',
       };
     } catch (e) {
       ctx.body = {
         data: null,
         status: false,
         message: (e as Error).message,
       };
     }
   };
 
   // 删除项目
   delete = async (ctx: Koa.Context) => {
     try {
       const { id } = ctx.request.body;
       await this.service.delete(id as string);
       ctx.body = {
         data: null,
         status: true,
         message: '',
       };
     } catch (e) {
       ctx.body = {
         data: null,
         status: false,
         message: (e as Error).message,
       };
     }
   };
 }
 
 export default new ProjectController();
 